import { AccountApi, fetchAccountList } from '@/api/userCenter/authority/account'
import { GetAccountsReq } from '@/api/userCenter/authority/model/accountModel'
import {
  EditOrganizationReq,
  OrganizationItem,
  UpdateOrgMemberReq,
} from '@/api/userCenter/authority/model/organizationModel'
import {
  editOrg,
  findOrgMember,
  OrganizationApi,
  updateOrgMenber,
} from '@/api/userCenter/authority/organization'
import { useStore } from '@/store'
import { computed, reactive } from '@vue/runtime-core'
import { message } from 'ant-design-vue'
import { EditOrgDialogState } from '../types'

/**
 * 处理编辑组织弹框相关逻辑
 * @param searchApi
 * @returns
 */
const useEditOrgDialog = (searchApi: AsyncFun) => {
  const store = useStore()
  const editOrgState = reactive<EditOrgDialogState>({
    visiable: false,
    currentId: '',
    allAccountList: [],
    formData: {
      name: '',
      code: '',
      pid: '',
      level: 0,
      note: '',
      managerId: '',
      phone: '',
      memberIds: [],
    },
  })

  /**
   * 打开编辑弹框
   * @param item 当前需要编辑的组织信息
   */
  const openEditOrgDialog = async (item: OrganizationItem) => {
    // 查询系统内所有成员列表
    const reqData: GetAccountsReq = {
      size: -1,
      current: 1,
    }
    const { data } = await fetchAccountList(reqData)
    editOrgState.allAccountList = data.records.map((item) => {
      return {
        value: item.id,
        label: item.name,
        title: item.name,
        disabled: !item.enabled,
      }
    })
    // 查询当前组织所有成员
    const { data: orgMenberData } = await findOrgMember(item.id)
    if (orgMenberData) {
      editOrgState.formData.memberIds = orgMenberData.records.map(
        (item) => item.id
      )
    } else {
      editOrgState.formData.memberIds = []
    }
    editOrgState.currentId = item.id
    editOrgState.formData.name = item.name
    editOrgState.formData.code = item.code
    editOrgState.formData.pid = item.pid
    editOrgState.formData.level = item.level
    editOrgState.formData.note = item.note
    editOrgState.formData.managerId = item.managerId
    editOrgState.formData.phone = item.phone

    editOrgState.visiable = true
  }
  const fetchAccountListLoading = computed(() =>
    store.getters['global/isOpLoading'](AccountApi.GET_ACCOUNT_LIST)
  )
  const findOrgMemberLoading = computed(() =>
    store.getters['global/isOpLoading'](OrganizationApi.FIND_ORG_MEMBER)
  )

  /**
   * 确认编辑内容
   */
  const enterEditDialog = async () => {
    const { memberIds, ...resForm } = editOrgState.formData
    const reqOrgData: EditOrganizationReq = {
      id: editOrgState.currentId,
      ...resForm,
    }
    await editOrg(reqOrgData)
    const reqOrgMemberData: UpdateOrgMemberReq = {
      id: editOrgState.currentId,
      userIds: memberIds,
    }
    await updateOrgMenber(reqOrgMemberData)
    message.success('编辑成功！')
    editOrgState.visiable = false
    await searchApi()
  }
  const editOrgLoading = computed(() =>
    store.getters['global/isOpLoading'](OrganizationApi.EDIT_ORGANIZATION)
  )
  const updateOrgMenberLoading = computed(() =>
    store.getters['global/isOpLoading'](OrganizationApi.UPDATE_ORG_MEMBER)
  )

  return {
    editOrgState,
    openEditOrgDialog,
    enterEditDialog,
    editOrgLoading,
    fetchAccountListLoading,
    updateOrgMenberLoading,
    findOrgMemberLoading,
  }
}

export default useEditOrgDialog
